package com.java.jigsaw.ui;

import cn.hutool.core.io.FileUtil;
import com.java.jigsaw.domain.User;
import com.java.jigsaw.util.CodeUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

//注册界面
public class RegisterJFrame extends JFrame implements MouseListener {
    ArrayList<User> allUsers;

    JButton confirm = new JButton();
    JButton reset = new JButton();

    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    JPasswordField rPassword = new JPasswordField();

    //对应两个密码框的显示
    JLabel visual = new JLabel();
    JLabel invisible = new JLabel();
    JLabel visual1 = new JLabel();
    JLabel invisible1 = new JLabel();

    public RegisterJFrame(ArrayList<User> allUsers){
        this.allUsers = allUsers;
        //初始化界面
        initJFrame();
        //在这个界面中添加内容
        initView();
        //让当前界面显示出来
        this.setVisible(true);
    }

    public void initView() {
        //添加注册用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("image/register/注册用户名.png"));
        usernameText.setBounds(85, 125, 79, 17);
        this.getContentPane().add(usernameText);
        //添加用户名输入框
        username.setBounds(182, 115, 194, 40);
        //设置透明背景色
        username.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(username);
        JLabel usernamePicture = new JLabel(new ImageIcon("image/register/对话框.png"));
        usernamePicture.setBounds(180, 115, 198, 40);
        this.getContentPane().add(usernamePicture);

        //添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("image/register/注册密码.png"));
        passwordText.setBounds(100, 185, 64, 16);
        this.getContentPane().add(passwordText);
        //密码输入框
        visual = new JLabel(new ImageIcon("image/login/已移除背景的显示密码.png"));
        visual.setBounds(340, 186, 29, 18);
        visual.addMouseListener(this);
        this.getContentPane().add(visual);
        invisible = new JLabel(new ImageIcon("image/login/已移除背景的显示密码按下.png"));
        invisible.setBounds(340, 186, 29, 18);
        invisible.addMouseListener(this);
        this.getContentPane().add(invisible);
        invisible.setVisible(false);
        password.setBounds(182, 175, 194, 40);
        password.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(password);
        JLabel passwordPicture = new JLabel(new ImageIcon("image/register/对话框.png"));
        passwordPicture.setBounds(180, 175,198, 40);
        this.getContentPane().add(passwordPicture);

        //添加再次输入密码文字
        JLabel rPasswordText = new JLabel(new ImageIcon("image/register/再次输入密码.png"));
        rPasswordText.setBounds(68, 245, 96, 17);
        this.getContentPane().add(rPasswordText);
        //密码输入框
        visual1 = new JLabel(new ImageIcon("image/login/已移除背景的显示密码.png"));
        visual1.setBounds(340, 246, 29, 18);
        visual1.addMouseListener(this);
        this.getContentPane().add(visual1);
        invisible1 = new JLabel(new ImageIcon("image/login/已移除背景的显示密码按下.png"));
        invisible1.setBounds(340, 246, 29, 18);
        invisible1.addMouseListener(this);
        this.getContentPane().add(invisible1);
        invisible1.setVisible(false);
        rPassword.setBounds(182, 235, 194, 40);
        rPassword.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(rPassword);
        JLabel rPasswordPicture = new JLabel(new ImageIcon("image/register/对话框.png"));
        rPasswordPicture.setBounds(180, 235,  198, 40);
        this.getContentPane().add(rPasswordPicture);

        //添加注册按钮
        confirm.setBounds(90, 310, 128, 47);
        confirm.setIcon(new ImageIcon("image/register/注册按钮.png"));
        confirm.setBorderPainted(false);
        confirm.setContentAreaFilled(false);
        confirm.addMouseListener(this);
        this.getContentPane().add(confirm);

        //添加重置按钮
        reset.setBounds(256, 310, 128, 47);
        reset.setIcon(new ImageIcon("image/register/重置按钮.png"));
        reset.setBorderPainted(false);
        reset.setContentAreaFilled(false);
        reset.addMouseListener(this);
        this.getContentPane().add(reset);

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image/register/background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }

    public void initJFrame() {
        this.setSize(470,417);
        this.setTitle("拼图 注册");
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    public boolean containsUser(String s){
        for(User u : allUsers){
            if(u.getUsername().equals(s)){
                return true;
            }
        }
        return false;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        String usernameInput = username.getText();
        String passwordInput = new String(password.getPassword());
        String rPasswordInput = new String(rPassword.getPassword());

        if(e.getSource() == confirm){
            String passwordText = new String(password.getPassword());
            String rPasswordText = new String(rPassword.getPassword());
            if(username.getText().length() == 0 || passwordText.length() == 0 || rPasswordText.length() == 0){
                LoginJFrame.showJDialog("用户名和密码不能为空");
                return;
            }
            if(!passwordText.equals(rPasswordText)){
                LoginJFrame.showJDialog("两次密码输入不一致");
                return;
            }
            if(!username.getText().matches("[a-zA-Z0-9]{4,16}")){
                LoginJFrame.showJDialog("用户名不符合规则");
                return;
            }
            if(!passwordText.matches("\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[a-z])\\S*")){
                LoginJFrame.showJDialog("密码不符合规则，至少包含1个小写字母，1个数字，长度至少6位");
                return;
            }
            if(containsUser(username.getText())){
                LoginJFrame.showJDialog("用户名已经存在，请重新输入");
                return;
            }
            allUsers.add(new User(username.getText(),new String(password.getPassword())));
            FileUtil.writeLines(allUsers,"/Applications/IDEA/jigsaw/userinfo.txt","UTF-8");
            LoginJFrame.showJDialog("注册成功");
            this.setVisible(false);
            new LoginJFrame();
        }else if(e.getSource() == reset){
            username.setText("");
            password.setText("");
            rPassword.setText("");
        }else if(e.getSource() == visual){
            visual.setVisible(false);
            invisible.setVisible(true);
            password.setEchoChar((char) 0);
        }else if(e.getSource() == invisible){
            visual.setVisible(true);
            invisible.setVisible(false);
            password.setEchoChar(LoginJFrame.sign);
        }else if(e.getSource() == visual1){
            visual1.setVisible(false);
            invisible1.setVisible(true);
            rPassword.setEchoChar((char) 0);
        }else if(e.getSource() == invisible1){
            visual1.setVisible(true);
            invisible1.setVisible(false);
            rPassword.setEchoChar(LoginJFrame.sign);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
